/**
 * @author Daniel
 */

//Ajax codez!!!
var queueing	= false;
var queue		= new Array();

//createCon initializes a connection and returns it for use
function initCon()
{
	var connection;
	
	if (window.XMLHttpRequest)
	{
		//code for IE7+, Firefox, Chrome, Opera, Safari
		connection		=new XMLHttpRequest();
	}
	else
	{
		//code for IE6, IE5
		connection		=new ActiveXObject("Microsoft.connection");
	}
	return connection;
}

//Ajax request method
function ajax(args)
{
	var request	= new Array();
	
	//Exception if we don't have an URL
	if(typeof args['url'] == "undefined")
	{
		throw "ajax(): Can't make an AJAX request without an url";
	}	
	
	//set defaults
	request['url']		= args['url'];
	request['type']		= "GET";
	request['data']		= "";
	request['async']	= true;
	request['success']	= false;
	
	//get request type
	if(typeof args['type'] != "undefined")
	{
		request['type']	= args['type'].toUpperCase;
	}
	//get any data
	if(typeof args['data'] != "undefined")
	{
		input	= args['data'];
		for(var key in input)
		{
			request['data']	+= key + "=" + input[key] + "&";
		}
		request['data']	= request['data'].substring(0, request['data'].length-1);
	}
	//get async
	if(typeof args['async'] != "undefined")
	{
		request['async'] = args['async'];
	}
	//get on success function
	if(typeof args['success'] != "undefined")
	{
		request['success'] = args['success'];
	}
	
	//queue the request
	queue.push(request);
		
	if(!queueing)
	{
		//Start processing the queue
		handleQueue();
	}
}

//queue handler
function handleQueue()
{
	//handle current item and remove from queue
	queueing	= true;
	var item;
	while(item = queue.shift())
	{
		if(item['type'] == "GET")
		{
			ajaxGet(item['url'], item['data'], item['async'], item['success']);
		}
		else
		{
			ajaxPost(item['url'], item['data'], item['async'], item['success']);
		}
	}
	queueing	= false;
}

//get request
function ajaxGet(url, data, async, success)
{
	var connection = initCon();
	if(data != "")
	{
		connection.open("GET",url + data,async);
	}
	else
	{
		connection.open("GET", url,async);
	}
	
	connection.onreadystatechange=function()
	{
		if(connection.readyState==4 && connection.status==200)
	  	{
	    	if(success != false)
	  		{
	  			success(connection);
	  		}
		}
		else if(connection.readyState==4 && connection.status==404)
		{
			document.getElementById('console').innerHTML += "404: " + url + data + " was not found.";
		}
	}
	
	connection.send();
}

//post request
function ajaxPost(url, data, async, success)
{
	var connection = initCon();
	if(data != "")
	{
		connection.open("POST",url,async);
		connection.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	}
	else
	{
		connection.open("POST",url,async);
	}
	
	connection.onreadystatechange=function()
	{
		if(connection.readyState==4 && connection.status==200)
	  	{
	  		var xml		= connection.responseXML;
	  		var html	= connection.responseText;
	  		if(success != false)
	  		{
	  			success(connection);
	  		}
		}
		else if(connection.readyState==4 && connection.status==404)
		{
			document.getElementById('console').innerHTML += "404: " + url + " was not found.";
		}
	}
	
	connection.send(data);
}